﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using EmailHelper.view;
using EmailHelper.model;
using System.Windows.Media;


namespace EmailHelper
{


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public List<EmailsModel> Emails = new List<EmailsModel>();
        public MainWindow()
        {
            InitializeComponent();
        }

 
        /// <summary>
        /// 失去焦点添加集合中的元素
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TB_tos_LostFocus(object sender, RoutedEventArgs e)
        {
            AddEmailsToList(TB_tos.Text.ToString());
            LB_ShowEmails.ItemsSource = Emails;
        }

        /// <summary>
        /// 获得焦点清除集合中的元素
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TB_tos_GotFocus(object sender, RoutedEventArgs e)
        {
            LB_ShowEmails.ItemsSource = null;
            Emails.Clear();
        }

        /// <summary>
        /// 向集合中添加元素
        /// </summary>
        /// <param name="strEmails"></param>
        public void AddEmailsToList(String strEmails)
        {
            bool isRemove = false;
            int count = 1;
            List<String> tempList = new List<String>();
            tempList.AddRange(strEmails.Split(','));
            for (int i = 0; i < tempList.Count; i++)
            {
                if (tempList[i].Contains("@") && tempList[i].Contains(".com"))
                {
                    Emails.Add(new EmailsModel() { Email = tempList[i],Count = count});
                    count++;
                }
                else
                {
                    tempList.RemoveAt(i);
                    isRemove = true;
                }
            }
            if (isRemove)
            {
                MessageBox.Show("部分收件人邮箱格式错误,以为您自动去掉", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_send_Click(object sender, RoutedEventArgs e)
        {
            String cc =null;
            List<String> toEmails = new List<string>();
            foreach (EmailsModel item in Emails)
            {
                toEmails.Add(item.Email);
            }

            cc = TB_CC.IsEnabled?TB_CC.Text.ToString():null;

            try
            {
                bool isSend = tool.AllEmailHelper.sendEmail(toEmails, cc, TB_from.Text.ToString(), TB_title.Text.ToString(),(TB_Content.Selection.Start.Parent as Run).Text.ToString(), TB_showFilePath.Text.ToString());
                if (isSend)
                {
                    MessageBox.Show("发送成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("发送失败", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch 
            {
                MessageBox.Show("请输入完整信息", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            
        }

        /// <summary>
        /// 软件配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void softWareConfig_Click(object sender, RoutedEventArgs e)
        {
            Config config = new Config();
           
            config.ShowDialog();
        }

        private void btn_addFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.ShowDialog();
            TB_showFilePath.Text = dlg.FileName;
        }

        private void btn_clear_Click(object sender, RoutedEventArgs e)
        {
            TB_title.Text = String.Empty;
            TB_tos.Text = String.Empty;
            TB_showFilePath.Text = "暂无附件";
            TB_from.Text = String.Empty;
            if (CB_IsCC.IsChecked == true)
            {
                TB_CC.Text = String.Empty;
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            GetEmailsFromDataBase getEmailsFromDataBase = new GetEmailsFromDataBase();
            getEmailsFromDataBase.setValueHandler += new setValue(set);
            getEmailsFromDataBase.ShowDialog();
        }

        public void set(List<EmailsModel> value)
        {
            Emails.Clear();
            LB_ShowEmails.ItemsSource = null;
            Emails = value;
            LB_ShowEmails.ItemsSource = Emails;
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            about About = new about();
            About.ShowDialog();
        }
    }
}
